package com.atguigu.eduservice.controller.front;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.Chapter.ChapterVo;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class CourseFrontController {
    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    EduChapterService eduChapterService;

    @Autowired
    OrderClient orderClient;

    @PostMapping("/getCoursePage/{page}/{limit}")
    public R getCoursePage(@PathVariable int page, @PathVariable int limit,
                           @RequestBody(required = false)CourseFrontVo courseFrontVo){
        Page<EduCourse> eduCoursePage= new Page<>(page,limit);
        Map<String,Object> map=eduCourseService.getCourseFrontList(eduCoursePage,courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo=eduCourseService.getFrontCourseInfo(courseId);
        //根据课程id查回章节和小节
        List<ChapterVo> chapterVideo = eduChapterService.getChapterVideo(courseId);
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        Boolean butCOurse = orderClient.isButCOurse(courseId,memberIdByJwtToken);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideo",chapterVideo).data("isBur",butCOurse);
    }

    //远程调用，根据课程id查询课程
    @PostMapping("getCourserOrder/{id}")
    public CourseWebVoOrder getCourseOrder(@PathVariable String id){
        CourseWebVo courseInfo = eduCourseService.getFrontCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
